package com.cloudptt.wp.ui.more;

import com.cloudptt.api.pjsip.api.SipConfigManager;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.config.call.CallConfig;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;

public class TimerSetting extends PreferenceActivity
		implements OnPreferenceChangeListener, OnPreferenceClickListener, OnSharedPreferenceChangeListener, OnClickListener {

	public static final String THIS_FILE = "TimerSetting";
	
	 private EditTextPreference pref_timer_nego = null;
	 private EditTextPreference pref_timer_keepAlive_wifi = null;
	 private EditTextPreference pref_timer_keepAlive_mobile = null;
	 private EditTextPreference pref_frame_per_packet = null;
	 private EditTextPreference pref_timer_option = null;
	 private EditTextPreference pref_timer_bln = null;
	 
	 private SwitchPreference pref_battery_android = null;
	 private SharedPreferences sp = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(getResources().getColor(R.color.app_theme_color_primary));
		super.onCreate(savedInstanceState);
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.app_theme_color_primary));
		getActionBar().hide();
		setContentView(R.layout.timer_setting);
		ImageView iv = (ImageView) findViewById(R.id.back_up_icon);
		iv.setOnClickListener(this);
		addPreferencesFromResource(R.xml.prefs_call_timer);
		TextView tv = (TextView) findViewById(R.id.titleTv);
		tv.setText(R.string.timer_setting);
		this.sp = PreferenceManager.getDefaultSharedPreferences(TimerSetting.this);
		this.sp.registerOnSharedPreferenceChangeListener(this);

		initSummary();
		
		
	}
	
	private void initSummary()
	{
		Resources res = this.getResources();
		
//		String key = res.getString(R.string.pref_timer_nego);
//		pref_timer_nego = (EditTextPreference)getPreferenceScreen().findPreference(key); 
//		String value = sp.getString(key, "" + NegotiateTimer.Default_Timer_Nego);
//		pref_timer_nego.setSummary(value);
//		
//		key = res.getString(R.string.pref_timer_keepAlive_wifi);
//		pref_timer_keepAlive_wifi = (EditTextPreference)getPreferenceScreen().findPreference(key); 
//		value = sp.getString(key, "" + NegotiateTimer.Default_Timer_KeepAlive_Wifi);
//		pref_timer_keepAlive_wifi.setSummary(value);
//		
//		key = res.getString(R.string.pref_timer_keepAlive_mobile);
//		pref_timer_keepAlive_mobile = (EditTextPreference)getPreferenceScreen().findPreference(key); 
//		value = sp.getString(key, "" + NegotiateTimer.Default_Timer_KeepAlive_Mobile);
//		pref_timer_keepAlive_mobile.setSummary(value);
		
		pref_frame_per_packet = (EditTextPreference) getPreferenceScreen( ).findPreference("timer_option");
		String value = sp.getString( SipConfigManager.FRAMES_PER_PACKET_SUFFIX, "1" );
		pref_frame_per_packet.setSummary( value );
		
		pref_timer_option = (EditTextPreference) getPreferenceScreen( ).findPreference("timer_option");
		value = sp.getString( CallConfig.Timer_Option, "" + CallConfig.Default_Timer_Option );
		pref_timer_option.setSummary( value );
		
		pref_timer_bln = (EditTextPreference) getPreferenceScreen( ).findPreference("timer_bln");
		value = sp.getString( CallConfig.Timer_BLN, "" + CallConfig.Default_Timer_BLN );
		pref_timer_bln.setSummary( value );
		
		pref_battery_android = (SwitchPreference) getPreferenceScreen( ).findPreference("battery_android");
		//boolean value = sharedPreferences.getBoolean(key, false);
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		Log.i(THIS_FILE, "onPreferenceChange:newValue="+newValue);
		return true;
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		Log.i(THIS_FILE, "onSharedPreferenceChanged:key="+key );
		Resources res = this.getResources();
		String negoKey = res.getString(R.string.pref_timer_nego);
		String wifiKeepAliveKey = res.getString(R.string.pref_timer_keepAlive_wifi);
		String mobileKeepAliveKey = res.getString(R.string.pref_timer_keepAlive_mobile);
		
		if(key.equals(negoKey)
				|| key.equals(wifiKeepAliveKey)
				|| key.equals(mobileKeepAliveKey) 
				|| key.equals( SipConfigManager.FRAMES_PER_PACKET_SUFFIX )
				|| key.equals( CallConfig.Timer_Option )
				|| key.equals(CallConfig.Timer_BLN))
		{
			initSummary();
		}
		else if(key.equals("battery_android"))
		{
			SysApplication app = ((SysApplication) getApplicationContext());
			Product product = app.getProduct();
//			SysConfig config = product.getSysConfig();
//			boolean value = sharedPreferences.getBoolean(key, true);
//			if(value)
//			{
//				config.setBatteryType(SysConfig.BATTERY_TYPE_ANDROID);
//			}
//			else
//			{
//				config.setBatteryType(SysConfig.BATTERY_TYPE_DEFIND);
//			}
		}
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		Log.i(THIS_FILE, "onPreferenceClick");
		return false;
	}
	
	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
		Log.i(THIS_FILE, "onPreferenceTreeClick");
		
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if(id == R.id.back_up_icon) 
		{
			this.finish();
		}
	}
}
